from sklearn.datasets import load_iris
from sklearn.metrics import accuracy_score  # 计算分类准确率
from sklearn.model_selection import train_test_split
from sklearn.naive_bayes import MultinomialNB  # 多项式贝叶斯分类器

if __name__ == '__main__':
    iris = load_iris()
    features = iris.data
    labels = iris.target
    # 划分训练集与测试集
    features_train, features_test, labels_train, labels_test = \
        train_test_split(features, labels, test_size=0.2, random_state=42)
    # 多项式贝叶斯
    multinomial_nb = MultinomialNB()
    multinomial_nb.fit(features_train, labels_train)
    labels_test_predict = multinomial_nb.predict(features_test)
    accuracy_test = accuracy_score(labels_test, labels_test_predict)
    print("分类准确率:", accuracy_test)
